// miniprogram/pages/team_list/team_list.js
const app = getApp();
const request = require('../../utils/request.js');
const config = require('../../utils/config');

Page({
  /**
   * 页面的初始数据
   */
  data: {
    colorTheme: app.globalData.colorTheme,
    animationTime: 0.1,
    teamList: [],
    pageNum: 1,
    pageSize: 50,
    params: {},
    isResult: true
  },

  /**
   * 加载队友列表
   */
  listTeamByParam: function (pageNum, pageSize, params) {
    console.log(params);
    console.log(pageNum);
    console.log(pageSize);
    let that = this;
    params = params ? params : {};
    let url = config.SERVER_URL + 'index/listTeamByParam?pageNum=' + pageNum + '&pageSize=' + pageSize;
    wx.showLoading({
      title: '加载ing',
    })
    request.requestGetApi(url, {}, params, 'application/json').then(res => {
      if(res.status === config.STATUS_OK){
        let isResult = res.data.rows.length >= pageSize;
        that.setData({
          isResult,
          teamList: that.data.teamList.concat(res.data.rows)
        });
      };
    }).catch(err => {
      app.failFun(err);
    }).finally((res) => {
      app.completeFun(res);
    });
    this.setData({
      params: params
    })
  },

  /**
 * 查看用户展示页
 */
  goTeamShowPage: function (e) {
    let teamId = e.currentTarget.dataset.teamid;
    wx.navigateTo({
      url: '/pages/team-show-info/team-show-info?teamId=' + teamId,
    })
  },

  /**
   * 搜索
   */
  searchSubmit: function (e) {
    let params = e.detail.value;
    this.setData({
      teamList: [],
      params: {},
      pageNum: 1
    });
    this.listTeamByParam(this.data.pageNum, this.data.pageSize, params);
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.listTeamByParam(this.data.pageNum, this.data.pageSize);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      teamList: []
    });
    this.listTeamByParam(this.data.pageNum, this.data.pageSize);

    wx.stopPullDownRefresh();
  },

  /**
   * 拉至底部
   */
  scrolltolower: function () {
    if(!this.data.isResult){
      return;
    }
    let pageNum = this.data.pageNum + 1;
    this.setData({
      pageNum: pageNum
    });
    this.listTeamByParam(pageNum, this.data.pageSize, this.data.params);
  },


})